// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial

import { CupertinoFontSettings, CupertinoPalette } from "styling.slint";
import { FocusBorder } from "components.slint";

export component Button {
    in property <string> text;
    in property <image> icon;
    in property <bool> primary;
    in property <bool> enabled <=> i-touch-area.enabled;
    in property <bool> checkable;
    in property <bool> colorize-icon;
    out property <bool> has-focus: i-focus-scope.has-focus;
    out property <bool> pressed: self.enabled && i-touch-area.pressed;
    in-out property <bool> checked;

    callback clicked;

    private property <brush> background: primary && root.enabled ? CupertinoPalette.accent-background : CupertinoPalette.control-background;
    private property <brush> text-color: primary && root.enabled ? CupertinoPalette.accent-foreground : CupertinoPalette.control-foreground;

    min-width: max(20px, i-layout.min-width);
    min-height: max(20px, i-layout.min-height);
    horizontal-stretch: 0;
    vertical-stretch: 0;
    accessible-label: text;
    accessible-role: button;
    forward-focus: i-focus-scope;

    states [
        disabled when !i-touch-area.enabled : {
            root.text-color: CupertinoPalette.foreground-secondary;
            root.background: CupertinoPalette.quaternary-control-background;
        }
        pressed when root.pressed : {
            root.background: root.primary ? CupertinoPalette.secondary-accent-background : CupertinoPalette.secondary-control-background;
        }
        checked when root.checked : {
            root.text-color: CupertinoPalette.secondary-accent-background;
        }
    ]

    animate background { duration: 150ms; }

    FocusBorder {
        x: (parent.width - self.width) / 2;
        y: (parent.height - self.height) / 2;
        width: parent.width + 6px;
        height: parent.height + 6px;
        border-radius: 8px;
        has-focus: root.has-focus;
    }

    if (root.primary && root.enabled) : Rectangle {
        drop-shadow-blur: 3px;
        drop-shadow-color: #00000066;
        drop-shadow-offset-y: 0.5px;
        border-radius: 5px;
        background: root.background;

        Rectangle {
            drop-shadow-blur: 2px;
            drop-shadow-color: #00000026;
            drop-shadow-offset-y: 1px;
            border-radius: parent.border-radius;
            background: root.background;
        }

        Rectangle {
            drop-shadow-blur: 1px;
            drop-shadow-color: #00000026;
            drop-shadow-offset-y: 0.5px;
            border-radius: parent.border-radius;
            background: root.background;
        }

        Rectangle {
            border-radius: parent.border-radius;
            background: CupertinoPalette.dimmer;
            opacity: 0.17;
        }
    }

    if (!root.primary || !root.enabled) : Rectangle {
        drop-shadow-blur: 0.25px;
        drop-shadow-color: #00000066;
        drop-shadow-offset-y: 0.25px;
        border-radius: 5px;
        background: root.background;

        Rectangle {
            drop-shadow-blur: 1px;
            drop-shadow-color: #00000026;
            drop-shadow-offset-y: 1px;
            border-radius: parent.border-radius;
            background: root.background;
            border-width: 1px;
            border-color: CupertinoPalette.decent-border;
            opacity: root.enabled ? 1 : 0.5;
        }
    }

    i-layout := HorizontalLayout {
        padding-left: 8px;
        padding-right: 8px;
        padding-top: 4px;
        padding-bottom: 4px;
        spacing: 4px;
        alignment: center;

        if (root.icon.width > 0 && root.icon.height > 0) : Image {
            y: (parent.height - self.height) / 2;
            source <=> root.icon;
            width: 13px;
            opacity: root.enabled ? 1 : 0.5;
            colorize: root.colorize-icon ? root.text-color : transparent;
        }

        if (root.text != "") : Text {
            opacity: root.enabled ? 1 : 0.5;
            font-size: CupertinoFontSettings.body.font-size;
            font-weight: CupertinoFontSettings.body.font-weight;
            horizontal-alignment: center;
            vertical-alignment: center;
            text: root.text;
            color: root.text-color;

            animate color { duration: 150ms; }
        }
    }

    i-touch-area := TouchArea {
        clicked => {
            if (root.checkable) {
                root.checked = !root.checked;
            }
            root.clicked();
        }
    }

    i-focus-scope := FocusScope {
        x: 0;
        width: 0; // Do not react on clicks
        enabled <=> root.enabled;

        key-pressed(event) => {
            if (event.text == " " || event.text == "\n") {
                i-touch-area.clicked();
                 return accept;
            }

            return reject;
        }
    }
}
